<?php
namespace app\user\controller;
use \think\Controller;
use \think\Request;
use \think\Session;
use app\user\traits\models\UserInfoCheck;
use app\common\traits\controller\TraitController;
use Carbon\Carbon;
use think\Hook;
use app\common\controller\Api;
use app\user\traits\controllers\TraitUser;
/**
 * 前台房车分享
 */
class SetCaravan extends Api
{  
    use UserInfoCheck;
    use TraitUser;

    //前置操作
    protected $beforeActionList = [
        'checkLogin',
        'checkcaravan'
    ];
    /**
     * [checkcaravan 判断]
     * @param string $value [description]
     */
    public function checkcaravan()
    {
        //绑定房车-行为
        Hook::exec('app\\behavior\\Base','checkbindcard');
    }
    /**
     * [checkLogin 判断是否登陆]
     * @return [type] [description]
     */
    protected function checkLogin()
    {
        if (!model('Users')->checkLogin()) {
            Session::set('redirect_url', Request::instance()->url(true));
            $this->redirect('@user/login');
            exit;
        }
    }
    
    public function getShareCaravan()
    {
        $user_id = session('user.id');
        $caravan_id = input('id', model('Caravans')->where('user_id',$user_id)->value('id'));
        $this->assign('caravan_id',$caravan_id);
        $share = model('common/Caravans')->getCaravanStatus($caravan_id);//halt($share);
        $this->assign('share',$share['shares']);//获取日历表房车的分享状态
        $this->assign('trusts',$share['trusts']);//获取日历表房车会员托管状态
        //$this->assign('disables',$share['disables']);//获取日历表房车的订单状态
        $this->assign('sharedisables',$share['disables']['shares']);//获取日历表房车分享订单状态
        $this->assign('trustdisables',$share['disables']['trusts']);//获取日历表房车托管订单状态
        $this->assign('notrustorderdisables',$share['disables']['trustsnoorder']);//获取日历表房车无订单托管订单状态

        $renta = model('Calendar')->where('caravan_id',$caravan_id)->where('trusts','1')->column('single_day_price', 'datetimes');
        $this->assign('renta',json_encode($renta));

        $camp_id = model('Users')->where('id',$user_id)->value('camp_id');//halt($camp_id);
        $travel_money_base = model('Camps')->where('id',$camp_id)->value('travel_money_base');
        $camp_tel=model('Camps')->where('id',$camp_id)->value('tel');
        $this->assign('camp_tel',$camp_tel);//营地电话
        $this->assign('travel_money_base',$travel_money_base);//营地旅行币基数
        return view('setcaravan/caravanshare');
    }

    public function postCash()
    {
        $caravan_id = input('post.caravan_id');//halt($end_date);
        $cash = input('post.cash');
        $start_date = input('post.startdaterent');
        $end_date = input('post.endaterent');
        return $cash = $cash * (model('Calendar')->diffDays($start_date, $end_date, $abs = true) + 1);
    }

    public function postShareCaravan()
    {
        $caravan_id = input('post.caravan_id');
        $start_date = input('post.startdate');
        $end_date = input('post.endate');
        $today = date('Y-m-d');
        if ($start_date == '' || $end_date == '') {
            return $this->error('请选择分享时间');
        }
        
        $count = $this->setShare($caravan_id, $start_date, $end_date);
        if ($count) {
            $url = Session::pull('redirect_url');
            if ($url == '/wishs/create') {
                return $this->success('分享成功',$url);
            }
            if ($url == url("/filter/exlist",'','','www')) {
                return $this->success('分享成功',$url);
            }
            return $this->success('分享成功','/setcaravan/sharecaravan');
        }else{
            return $this->error('分享失败');
        }
        
        
    }

    public function postCancelShare()
    {
        $caravan_id = input('post.caravan_id');
        $start_date = input('post.csdate');
        $end_date = input('post.cedate');
        
        $count = $this->setHome($caravan_id, $start_date, $end_date);
        if ($count) {
            return $this->success('取消分享成功','/setcaravan/sharecaravan');
        }else{
                return $this->error('取消分享失败');
            }
    }

    public function getRentCaravan()
    {
        $user_id = session('user.id');
        $caravan_id = input('id', model('Caravans')->where('user_id',$user_id)->value('id'));
        $this->assign('caravan_id',$caravan_id);
        $integral = model('Caravans')->where('user_id',$user_id)->value('integral');
        $this->assign('integral',$integral);
        $camp_id = model('Users')->where('id',$user_id)->value('camp_id');
        $rent_money_base = model('Camps')->where('id',$camp_id)->value('rent_money_base');
        $this->assign('rent_money_base',$rent_money_base);

        
        $trusts = model('common/Caravans')->getCaravanStatus($caravan_id);
        $this->assign('share',$trusts['shares']);//获取日历表房车的分享状态
        $this->assign('trust',$trusts['trusts']);//获取日历表房车的托管状态
        //$this->assign('disables',$trusts['disables']);//获取日历表房车的订单状态
        $this->assign('sharedisables',$trusts['disables']['shares']);//获取日历表房车分享订单状态
        $this->assign('trustdisables',$trusts['disables']['trusts']);//获取日历表房车托管订单状态
        $this->assign('notrustorderdisables',$trusts['disables']['trustsnoorder']);//获取日历表房车无订单托管订单状态
        $camp_tel=model('Camps')->where('id',$camp_id)->value('tel');
        $this->assign('camp_tel',$camp_tel);//营地电话
        $renta = model('Calendar')->where('caravan_id',$caravan_id)->where('trusts','1')->column('single_day_price', 'datetimes');
        $this->assign('renta',json_encode($renta));

        return view('setcaravan/caravanrent');
    }

    public function postRentCaravan()
    {
        $caravan_id = input('post.caravan_id');
        $start_date = input('post.startdaterent');
        $end_date = input('post.endaterent');

        if ($start_date == '' || $end_date == '') {
            return $this->error('请选择托管时间');
        }

        $count = $this->setTrust($caravan_id, $start_date, $end_date);
        if ($count) {
            return $this->success('托管成功','/setcaravan/rentcaravan');
        }else{
            return $this->error('托管失败');
        }

    }

    
}

